package com.woniu.his.cms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.his.cms.common.CmsResultCode;
import com.woniu.his.cms.infra.model.NonDrug;
import com.woniu.his.cms.infra.model.NonDrugModel;
import com.woniu.his.cms.infra.mapper.NonDrugModelMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.his.cms.infra.model.NonDrugRelation;
import com.woniu.his.cms.service.NonDrugModelService;
import com.woniu.his.cms.service.NonDrugRelationService;
import com.woniu.his.cms.service.NonDrugService;
import com.woniu.his.cms.service.dto.CreateNonDrugModelDto;
import com.woniu.his.cms.service.dto.QueryNonDrugModelDto;
import com.woniu.his.cms.service.param.NonDrugModelSvParam;
import com.woniu.his.cms.service.param.QueryNonDrugModelSvParam;
import com.woniu.his.cms.service.param.UpdateNonDrugModelSvParam;
import com.woniu.his.exception.HisException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 非药品模版 服务实现类
 * </p>
 *
 * @author dx
 * @since 2022-11-28
 */
@Service
public class NonDrugModelServiceImpl extends ServiceImpl<NonDrugModelMapper, NonDrugModel> implements NonDrugModelService {

    @Resource
    private NonDrugRelationService nonDrugRelationService;

    @Resource
    private NonDrugService nonDrugService;

    /**
     * 新增非药品模板
     *
     * @param param
     * @return
     */
    @Override
    public CreateNonDrugModelDto createNonDrugModel(NonDrugModelSvParam param) {

        ifCanBeSaveOrUpdate(param,null);
        NonDrugModel nonDrugModel = BeanUtil.copyProperties(param, NonDrugModel.class);
        nonDrugModel.setCreateTime(new Date());
        nonDrugModel.setStatusNdm("ON");
        save(nonDrugModel);
        return BeanUtil.copyProperties(nonDrugModel, CreateNonDrugModelDto.class);
    }

    private void ifCanBeSaveOrUpdate(NonDrugModelSvParam param,Long id) {
        if (!(param.getScope().equals("PERSONAL") || param.getScope().equals("DEPT") || param.getScope().equals("HOSPITAL"))) {
            throw new HisException(CmsResultCode.NON_DRUG_MODEL_SCOPE_WRONG);
        }
        if (!(param.getType().equals("CHECK") || param.getType().equals("TEST") || param.getType().equals("DESPOSITION"))) {
            throw new HisException(CmsResultCode.NON_DRUG_MODEL_TYPE_WRONG);
        }
        if(id!=null){
            if (list(new QueryWrapper<NonDrugModel>().eq("code", param.getCode()).ne("id",id)).size() >= 1) {
                throw new HisException(CmsResultCode.NON_DRUG_MODEL_CODE_EXITS);
            }
            if (list(new QueryWrapper<NonDrugModel>().eq("name_non_drug_mdoel", param.getNameNonDrugMdoel()).ne("id",id)).size() >= 1) {
                throw new HisException(CmsResultCode.NON_DRUG_MODEL_NAME_EXITS);
            }
        }else {
            if (list(new QueryWrapper<NonDrugModel>().eq("code", param.getCode())).size() >= 1) {
                throw new HisException(CmsResultCode.NON_DRUG_MODEL_CODE_EXITS);
            }
            if (list(new QueryWrapper<NonDrugModel>().eq("name_non_drug_mdoel", param.getNameNonDrugMdoel())).size() >= 1) {
                throw new HisException(CmsResultCode.NON_DRUG_MODEL_NAME_EXITS);
            }
        }
    }


    /**
     * 修改非药品模板
     *
     * @param param
     * @return
     */
    @Override
    public CreateNonDrugModelDto updateModel(UpdateNonDrugModelSvParam param) {
        NonDrugModel nonDrugModel = getById(param.getId());
        if (nonDrugModel == null) {
            throw new HisException(CmsResultCode.NON_DRUG_MODEL_NOT_EXITS);
        }
        ifCanBeSaveOrUpdate(BeanUtil.copyProperties(param, NonDrugModelSvParam.class),param.getId());
        NonDrugModel nonDrugModel1 = BeanUtil.copyProperties(param, nonDrugModel.getClass());
        nonDrugModel1.setStatusNdm(nonDrugModel.getStatusNdm());
        nonDrugModel1.setCreateTime(nonDrugModel.getCreateTime());
        updateById(nonDrugModel1);
        return BeanUtil.copyProperties(nonDrugModel1, CreateNonDrugModelDto.class);
    }

    /**
     * @Description: 查询非药品模板
     * @Param:
     * @return:
     * @Date: 2022/11/30
     */
    @Override
    public List<QueryNonDrugModelDto> selectNonDrugModel(QueryNonDrugModelSvParam param) {

        QueryWrapper<NonDrugModel> wrapper = getNonDrugModelQueryWrapper(param);
        List<NonDrugModel> list = list(wrapper);
        if (list.size()<=0) {
            throw new HisException(CmsResultCode.NON_DRUG_MODEL_NOT_EXITS);
        }
        return list.stream().map(o -> {
            QueryNonDrugModelDto queryNonDrugModelDto = BeanUtil.copyProperties(o, QueryNonDrugModelDto.class);
            System.out.println(queryNonDrugModelDto);
            Long modelId = o.getId();
            List<NonDrugRelation> list2 = nonDrugRelationService.list(new QueryWrapper<NonDrugRelation>().eq("non_drug_model_id", modelId));
            if (list2.size() > 0) {
                list2.forEach(a -> {
                    Map<Long, String> map1 = new HashMap<>();
                    map1.put(a.getNonDrugId(), nonDrugService.getById(a.getNonDrugId()).getNonDrugName());
                    queryNonDrugModelDto.setRelationMap(map1);
                });
            }
            return queryNonDrugModelDto;
        }).collect(Collectors.toList());
    }

    private QueryWrapper<NonDrugModel> getNonDrugModelQueryWrapper(QueryNonDrugModelSvParam param) {
        QueryWrapper<NonDrugModel> wrapper = new QueryWrapper<>();

        if (param.getAim() != null&&!param.getAim().trim().equals("")) {
            wrapper.like("aim", param.getAim());
        }
        if (param.getCode() != null&&!param.getCode().trim().equals("")) {
            wrapper.like("code", param.getCode());
        }
        if (param.getNameNonDrugMdoel() != null&&!param.getNameNonDrugMdoel().trim().equals("")) {
            wrapper.like("name_non_drug_mdoel", param.getNameNonDrugMdoel());
        }
        System.out.println(2222);
        if (param.getOwnId() != null&&!param.getOwnId().equals("")) {
            System.out.println("132");
            wrapper.eq("own_id", param.getOwnId());
        }
        if (param.getType() != null&&!param.getType().trim().equals("")) {
            wrapper.eq("type", param.getType());
        }
        if (param.getScope() != null&&!param.getScope().trim().equals("")) {
            wrapper.eq("scope", param.getScope());
        }
        return wrapper;
    }


}
